//
//  CityViewController.m
//  EagleEye
//
//  Created by 18_李明明 on 15-1-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CityViewController.h"

@interface CityViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSArray *areaArr;
@property (nonatomic, strong) NSMutableArray *cityArr;
@property (nonatomic, strong) UITableView *cityTableView;
@property (nonatomic, strong) UITextField *cityTxt;
@end

@implementation CityViewController
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.cityArr = [NSMutableArray array];
        self.areaArr = [NSArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.city = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
    
    self.areaArr = [self.city componentsSeparatedByString:@"\n"];
    self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, WIDTH, HEIGHT - 90) style:UITableViewStyleGrouped];
    // Do any additional setup after loading the view.
    // 把城市从省市区中找出来
    for (NSString *tempStr in self.areaArr) {
        if ([tempStr hasPrefix:@"  "] && ![tempStr hasPrefix:@"    "])
        {
            NSString *str = [[tempStr componentsSeparatedByString:@" "] objectAtIndex:2];
            [self.cityArr addObject:str];
        }
    }
    [self.view addSubview:self.cityTableView];
    self.cityTableView.delegate = self;
    self.cityTableView.dataSource = self;
    // 添加一个返回的按钮
    UIButton *cancle = [UIButton buttonWithType:UIButtonTypeSystem];
    cancle.frame = CGRectMake(0, HEIGHT - 40, 50, 40);
//    [cancle setTitle:@"返回" forState:UIControlStateNormal];
    [cancle setBackgroundImage:[UIImage imageNamed:@"cancle.png"] forState:UIControlStateNormal];
    [self.view addSubview:cancle];
    [cancle addTarget:self action:@selector(targetCancle:) forControlEvents:UIControlEventTouchUpInside];
    // 添加一个label
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, WIDTH, 30)];
    name.text = @"全国城市";
    name.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:name];
    // 添加一个输入框
    self.cityTxt = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, WIDTH * 4 / 5, 36)];
    self.cityTxt.placeholder = @"请输入要搜索的城市";
    self.cityTxt.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.cityTxt];
    // 添加一个搜索按钮
    UIButton *search = [UIButton buttonWithType:UIButtonTypeSystem];
    search.frame = CGRectMake(WIDTH * 4 / 5, 50, WIDTH / 5, 36);
    [search setTitle:@"搜索" forState:UIControlStateNormal];
    [search addTarget:self action:@selector(targetSearch:) forControlEvents:UIControlEventTouchUpInside];
    search.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:search];
    self.cityTxt.delegate = self;
}
// 搜索按钮方法
- (void)targetSearch:(UIButton *)search
{
    [self.cityTxt resignFirstResponder];
    NSMutableArray *reduction = [NSMutableArray arrayWithArray:self.cityArr];
    NSMutableArray *tempArr = [NSMutableArray array];
    for (NSString *tempStr in self.cityArr) {
        if ([tempStr hasPrefix:self.cityTxt.text]) {
            [tempArr addObject:tempStr];
        }
    }
    self.cityArr = tempArr;
    [self.cityTableView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
//        NSLog(@"%@", reduction);
        self.cityArr = reduction;
    });
}
// 返回按钮方法
- (void)targetCancle:(UIButton *)cancle
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cityArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"0502"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"0502"];
    }
    
    cell.textLabel.text = [self.cityArr objectAtIndex:indexPath.row];
    return cell;
}
// 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    NSString *str = cell.textLabel.text;
  
    if ([str hasSuffix:@"市"]) {
        str = [str substringToIndex:str.length - 1];
    }else if([str hasSuffix:@"自治州"]) {
        str = [str substringToIndex:2];
    }else if ([str hasSuffix:@"地区"]){
        str = [str substringToIndex:str.length - 2];
    }
    self.cityBlock(str);
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
//    [self.navigationController popViewControllerAnimated:YES];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.cityTxt resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
